package com.leven.easyerp.util;

import java.util.List;
import java.util.Map;
import java.util.Set;

import com.leven.easyerp.exception.AssertionException;

/**
 * 断言辅助工具类
 * 
 * @author Leven.Chen
 */
public class AssertUtils {
	/**
	 * 断言对象为null
	 * 
	 * @param obj
	 */
	public static void assertNull(Object obj) {
		if (obj != null) {
			throw new AssertionException();
		}
	}

	/**
	 * 断言对象不为null
	 * 
	 * @param obj
	 */
	public static void assertNotNull(Object obj) {
		if (obj == null) {
			throw new AssertionException();
		}
	}

	/**
	 * 断言字符串是空串
	 * 
	 * @param str
	 */
	public static void assertBlank(String str) {
		if (str != null && str.trim().length() > 0) {
			throw new AssertionException();
		}
	}

	/**
	 * 断言字符串不是空串
	 * 
	 * @param str
	 */
	public static void assertNotBlank(String str) {
		assertNotNull(str);
		if (str.trim().length() == 0) {
			throw new AssertionException();
		}
	}

	/**
	 * 断言集合不为空
	 * 
	 * @param array
	 */
	public static void assertArrayNotEmpty(Object array) {
		assertNotNull(array);
		if (array instanceof List && ((List<?>) array).isEmpty()) {
			throw new AssertionException("List");
		} else if (array instanceof String[] && ((String[]) array).length == 0) {
			throw new AssertionException("String[]");
		} else if (array instanceof Map && ((Map<?, ?>) array).isEmpty()) {
			throw new AssertionException("Map");
		} else if (array instanceof Set && ((Set<?>) array).isEmpty()) {
			throw new AssertionException("Set");
		}
	}

	/**
	 * 断言两对象相同
	 * 
	 * @param o1
	 * @param o2
	 */
	public static void assertEquals(Object o1, Object o2) {
		if (o1 != o2) {
			if (o1 == null || o2 == null) {
				throw new AssertionException();
			} else if (!o1.equals(o2)) {
				throw new AssertionException();
			}
		}
	}

	/**
	 * 断言条件为真
	 * 
	 * @param condition
	 */
	public static void assertTrue(boolean condition) {
		if (!condition) {
			throw new AssertionException();
		}
	}

	/**
	 * 断言条件为假
	 * 
	 * @param condition
	 */
	public static void assertFalse(boolean condition) {
		if (condition) {
			throw new AssertionException();
		}
	}
}
